<?php

/*
 * Copyright (c) 2012 Aalto University and University of Helsinki
 * MIT License
 * see LICENSE.txt for more information
 */

include_once("model/Model.php");
require_once('config.inc');

class RestController {
  
  public $model;
 
  public function __construct() {
    $this->model = new Model();
  }

  public function return_error($code, $status, $message) {
    header("Status: $code");
    header("Content-type: text/plain");
    echo "$code $status : $message";
  }

  public function search() {
    if(isset($_GET['query']))
      $term = $_GET['query'];
    else
      return $this->return_error(400, "Bad Request", "query parameter missing");
    if(isset($_GET['maxhits']) && (!is_numeric($_GET['maxhits']) || $_GET['maxhits'] <= 0)) {
      return $this->return_error(400, "Bad Request", "maxhits parameter is invalid");
    }
    if(isset($_GET['offset']) && (!is_numeric($_GET['offset']) || $_GET['offset'] < 0)) {
      return $this->return_error(400, "Bad Request", "offset parameter is invalid");
    }
    if(isset($_GET['lang']) &&  !in_array($_GET['lang'], array('fi', 'sv', 'en')))
        return $this->return_error(400, "Bad Request", "lang parameter is invalid");
    
    $vocid = isset($_GET['vocab']) ? $_GET['vocab'] : null; # optional
    $lang = isset($_GET['lang']) ? $_GET['lang'] : null; # optional

    $maxhits = isset($_GET['maxhits']) ? ($_GET['maxhits']) : null; # optional
    $offset = isset($_GET['offset']) ? ($_GET['offset']) : 0; # optional

    $results = $this->model->searchConcepts($term, $vocid, $lang, $offset, $maxhits);
    // before serializing to JSON, get rid of the Vocabulary object that came with each resource
    foreach($results as &$res)
      unset($res['voc']);

    $ret = array(
        '@context' => array(
            'skos' => 'http://www.w3.org/2004/02/skos/core#',
            'onki' => 'http://schema.onki.fi/onki#',
            'uri' => '@id',
            'results' => array(
                '@id' => 'onki:results',
                '@type' => '@id',
                '@container' => '@list',
            ),
            'label' => 'skos:prefLabel',
            'altlabel' => 'skos:altLabel',
        ),
        'results' => $results,
    );
    
    if ($lang)
      $ret['@context']['@language'] = $lang;


    header("Access-Control-Allow-Origin: *"); // enable CORS
    header("Content-type: application/json");
    
    if (isset($_GET['callback'])) {
      // wrap with JSONP callback
      echo $_GET['callback'] . "(" . json_encode($ret) . ");";
    } else {
      echo json_encode($ret);
    }
  }

  public function concept() {
    if (isset($_GET['uri']) && isset($_GET['vocab'])) {
      $uri = $_GET['uri'];
      $vocab = $_GET['vocab'];
    }
    else
      return $this->return_error(400, "Bad Request", "query/vocab parameter missing");
    if (isset($_GET['format'])) {
      $format = $_GET['format'];
    } else {
       $accept_header = $_SERVER['HTTP_ACCEPT'];
       $accept_header = explode(',', $accept_header);
      
      $format = 'application/rdf+xml';
      if (in_array('text/turtle', $accept_header)) {
        $format = 'text/turtle';
        break;
      }
    }

    $results = $this->model->getRDF($vocab, $uri, $format);

//    header("Access-Control-Allow-Origin: *"); // enable CORS
    header("Content-type: " . $format);

    echo $results;
  }

  public function data($vocab) {
    if (!$vocab)
      return $this->return_error(400, "Bad Request", "vocab parameter missing");
    
    $url = $this->model->getVocabulary($vocab)->getDataURL();
    header("Location: " . $url);
  }
  
}
